package recite.word.service.base.util.ali;

import com.aliyun.alimt20181012.Client;
import com.aliyun.alimt20181012.models.TranslateGeneralRequest;
import com.aliyun.alimt20181012.models.TranslateGeneralResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import recite.word.service.base.util.common.Asserts;

@Service
public class TranslateUtil {
    @Autowired
    private Client myClient;

    private final String AccessKeyID2 = "LTAI5tPAEJDQ7aKoz2Hy6zzL";
    private final String AccessKeySecret2 = "uJDJAJ4h7tEHnOkXrKjKQk1FomZb8t";

    public String translate(String text) {
        try {
            TranslateGeneralRequest translateGeneralRequest = new TranslateGeneralRequest()
                    .setFormatType("text")
                    .setSourceLanguage("en")
                    .setTargetLanguage("zh")
                    .setSourceText(text)
                    .setScene("general");
            TranslateGeneralResponse translateGeneralResponse = myClient.translateGeneral(translateGeneralRequest);
            return translateGeneralResponse.body.data.translated;
        } catch (Exception e) {
            Asserts.isTrue(false,"翻译失败");
            return null;
        }
    }
}
